package com.itheima.controller;



import com.itheima.domain.Student;
import com.itheima.service.StudentService;
import com.itheima.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController //返回格式是json
@Slf4j //添加此注解后,就可以在类中使用一个log对象了
public class StudentController {

    @Autowired
    private StudentService studentService;

    //查询所有
    @GetMapping("/student")
    public Result findAll(){
        List<Student> studentList = studentService.findAll();
        return Result.success(studentList);
    }


    //新增
    @PostMapping("/student")
    public Result save(@RequestBody Student student){
        studentService.save(student);
        return Result.success();
    }



    //根据id查询
    @GetMapping("/student/{id}")
    public Result findById(@PathVariable("id") Integer id){
        Student student = studentService.findById(id);
        return Result.success(student);
    }

    //修改
    @PutMapping("/student")
    public Result update(@RequestBody Student student){
        studentService.update(student);
        return Result.success();
    }


    //删除
    @DeleteMapping("/student/{id}")
    public Result deleteById(@PathVariable("id") Integer id){
        studentService.deleteById(id);
        return Result.success();
    }



}
